#!/usr/bin/env python3.0

#The prime factors of 13195 are 5, 7, 13 and 29.
#
#What is the largest prime factor of the number 600851475143 ?

def factors_gen(lst=[2, 3]):
    for n in lst:
        yield n
    while True:
        n += 2
        yield n        
    
def largest_prime_factor(n):
    n = abs(n)    
    if n < 2:
        return None    
    factors = factors_gen()
    p = next(factors)
    while p * p <= n:
        while True:
            (q, r) = divmod(n, p)
            if r != 0:
                break
            else:
                n = q
        p = next(factors)
    return n

if __name__ == '__main__':
    n = 600851475143
    print('Result: {0}'.format(largest_prime_factor(n)))
    